home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 16 / AMIGAplus Sonderheft 16 (1998)(ICP)(DE)[!].iso / pd / anwendungen / rtgmaster_dev / demos / network / p2pserv.c < prev    next >
C/C++ Source or Header  |  1997-09-26  |  2KB  |  82 lines

  1. #include <clib/socket_protos.h>
  2. #include <pragmas/socket_pragmas.h>
  3. #include <clib/rtgextra_protos.h>
  4. #include <pragmas/rtgextra_pragmas.h>
  5. #include <clib/exec_protos.h>
  6. #include <pragmas/exec_pragmas.h>
  7. #include <stdio.h>
  8. #include <netdb.h>
  9. #include <rtgmaster/rtgtcpip.h>
  10. #define UDPDemo 1
  11.  
  12. // If you want to compile this demo to use
  13. // TCP instead of UDP, set UDPDemo to 0
  14.  
  15. struct Library *SocketBase=0;
  16. struct Library *RTGExtraBase=0;
  17.  
  18. void main()
  19. {
  20.  if (RTGExtraBase=OpenLibrary("rtgextra.library",0))
  21.  {
  22.   if (SocketBase=OpenLibrary("bsdsocket.library",0))
  23.   {
  24.    struct RTG_Socket *rs;
  25.    struct RTG_Socket *msgsock;
  26.    char buf[1024];
  27.    int rval;
  28.    long mode=1;
  29.  
  30. #ifndef UDPDemo
  31.    rs=OpenServer(SocketBase,3056,SOCK_STREAM,0);
  32. #else
  33.    rs=OpenServer(SocketBase,3056,SOCK_DGRAM,0);
  34. #endif
  35.  
  36.    //RtgIoctl(SocketBase,rs,&mode);
  37.  
  38.    while(1)
  39.    {
  40.  
  41. #ifndef UDPDemo
  42.     msgsock=RtgAccept(SocketBase,rs);
  43. #else
  44.     msgsock=rs;
  45. #endif
  46.  
  47.     do {
  48.         struct sockaddr_in *si;
  49.  
  50. // Note : As GetUDPName returns 0 for TCP connections, this code
  51. // works for TCP *and* UDP ...
  52.  
  53. // If you want a Connection with Multiple Client, WITHOUT
  54. // using RunServer (works only with UDP...) you can simply
  55. // open multiple Clients and then test, from which Client a
  56. // Message came using RtgInAdr (it can't differentiate
  57. // between multiple Clients running on the same machine, BTW).
  58.  
  59.         si=GetUDPName(SocketBase,msgsock);
  60.         rval=RtgRecv(SocketBase,msgsock,buf,si,1024);
  61.  
  62. #ifndef UDPDemo
  63.  
  64. #else
  65.         if (rval>0) printf("Message from : %s\n",RtgInAdr(SocketBase,si));
  66. #endif
  67.  
  68.         if (rval>0) RtgSend(SocketBase,msgsock,buf,si,rval);
  69.        } while (rval>0);
  70.  
  71. #ifndef UDPDemo
  72.     CloseClient(SocketBase,msgsock);
  73. #endif
  74.  
  75.    }
  76.    if (SocketBase) CloseLibrary(SocketBase);
  77.    if (RTGExtraBase) CloseLibrary(RTGExtraBase);
  78.   }
  79.  }
  80. }
  81.  
  82.